<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    var clientId = 10002;
    var  toClientId = 10001;

    var socket;
    //判断当前浏览器是否支持websocket
    if(window.WebSocket) {
        //go on
        socket = new WebSocket("ws://localhost:8888/ws");
        //相当于channelReado, ev 收到服务器端回送的消息
        socket.onmessage = function (ev) {
            if (ev.data.indexOf("Hello Netty") != -1) {
                console.log(ev.data);
                return;
            }
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }

        //相当于连接开启(感知到连接开启)
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启了.."

            setInterval(function(){ sendHeart() }, 5000);
        }

        //相当于连接关闭(感知到连接关闭)
        socket.onclose = function (ev) {

            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + "连接关闭了.."
        }
    } else {
        alert("当前浏览器不支持websocket")
    }

    /**
     * 和后端的 MessageVo 聊天模型对象保持一致
     * @param {Object} msgType
     * @param {Object} roomType
     * @param {Object} clientId
     * @param {Object} toClientId
     * @param {Object} remark
     * @param {Object} data
     */
    function MessageVo(msgType, roomType, clientId, toClientId, remark, data){
        this.msgType = msgType;
        this.roomType = roomType;
        this.clientId = clientId;
        this.toClientId = toClientId;
        this.remark = remark;
        this.data = data;
    }

    /**
     * 构建消息 ChatTextParamsVo 模型对象
     * @param {Object} chatText
     */
    function ChatTextParamsVo(chatText){
        this.chatText = chatText;
    }

    /**
     * 构建消息 ChatTextParamsVo 模型对象
     * @param {Object} chatText
     */
    function DeviceHeartBeatVo(timeSp){
        this.timeSp = timeSp;
    }

    //发送消息到服务器
    function sendHeart() {
        if(!window.socket) { //先判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket 发送消息

            var deviceHeartBeatVo = new DeviceHeartBeatVo(1000);
            var messageVo = new MessageVo(2, 0, clientId, null, "remark", deviceHeartBeatVo);
            var sendMsg = JSON.stringify(messageVo);
            socket.send(sendMsg);
            // socket.send(message);
        } else {
            alert("连接没有开启");
        }
    }
    //发送消息到服务器
    function send(message) {
        if(!window.socket) { //先判断socket是否创建好
            return;
        }
        if(socket.readyState == WebSocket.OPEN) {
            //通过socket 发送消息

            var chatTextParamsVo = new ChatTextParamsVo(message);
            var messageVo = new MessageVo(11, 1, clientId, toClientId, "remark", chatTextParamsVo);
            var sendMsg = JSON.stringify(messageVo);
            socket.send(sendMsg);
            // socket.send(message);
        } else {
            alert("连接没有开启");
        }
    }
</script>
<form onsubmit="return false">
    your name: 10002; send to 10001 <br>
    <input type="text" name="message">
    <br>
    <input type="button" value="发生消息" onclick="send(this.form.message.value)">
    <br>
    <br>
    收到消息：
    <br>
    <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
    <br>
    <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
</form>
</body>
</html>